package com.shijie.controller.erp;

import com.shijie.service.AdvertsService;
import com.shijie.domain.entity.Adverts;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.core.annotation.Log;
import com.shijie.core.annotation.RequiresPermissions;
import com.shijie.core.constants.PnConstant.BusinessType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.web.bind.annotation.*;

import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

import java.util.List;

/*
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/erp/adverts")
public class AdvertsController {

    @Resource
	private AdvertsService advertsService;

    @RequiresPermissions(value={"advert:list"})
	@GetMapping
    public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,@RequestParam(required=false) Integer pageId,
    		@RequestParam(required=false) Integer position,@RequestParam(required=false) Integer status) {
        PageHelper.startPage(page, pageSize);
        Condition condition = new Condition(Adverts.class);
        Example.Criteria criteria = condition.createCriteria();
        if(pageId != null){
        	criteria.andEqualTo("page", pageId);
        }
        if(position != null){
        	criteria.andEqualTo("position", position);
        }
        if(status != null){
        	criteria.andEqualTo("status", status);
        }
        List<Adverts> list = advertsService.selectByCondition(condition);
        PageInfo<Adverts> pageInfo = new PageInfo<>(list);
        return ResultGenerator.success(pageInfo);
	}

	@RequiresPermissions(value = { "advert:create","advert:eidt" })
	@Log(title = "广告管理", action = BusinessType.INSERT)
	@PostMapping
    public Result add(@RequestBody Adverts adverts) {
        advertsService.save(adverts);
        return ResultGenerator.success();
	}

	@RequiresPermissions(value = { "advert:delete" })
	@Log(title = "广告管理", action = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        advertsService.deleteById(id);
        return ResultGenerator.success();
	}

    @PutMapping
    public Result update(@RequestBody Adverts adverts) {
        advertsService.updateById(adverts);
        return ResultGenerator.success();
	}
}

